package com.meitianhui.convenitenbuy.module.mine.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseToolbarActivity;
import com.meitianhui.convenitenbuy.module.mine.contract.SetPayPasswordContract;
import com.meitianhui.convenitenbuy.module.mine.di.DaggerMineComponent;
import com.meitianhui.convenitenbuy.module.mine.di.MineModule;
import com.meitianhui.convenitenbuy.module.mine.presenter.SetPayPasswordPresenter;
import com.meitianhui.convenitenbuy.util.RxUtils;
import com.meitianhui.convenitenbuy.util.TipUtils;

import javax.inject.Inject;

import static com.meitianhui.convenitenbuy.util.JumpUtil.JUMP_USER_PHONE_NUMBER;

/**
 * Created by amy on 2017/11/9.
 */

public class SetPayPasswordActivity extends BaseToolbarActivity implements SetPayPasswordContract.View {

    private TextView mEtPaySettingTitle;
    private EditText mEtPayInputCode;
    private Button mBtnPayGainCode;
    private EditText mEtPayPassword1;
    private EditText mEtPayPassword2;
    private Button mBtPayConform;
    private String mUserPhoneNumber;
    @Inject
    SetPayPasswordPresenter mSetPayPasswordPresenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTbTitle("支付密码");
        initId();
        initRXClick();
        initUserInfo();
        isEnable(false);
        DaggerMineComponent.builder().appComponent(App.getInstance().getAppComponent()).mineModule(new MineModule(this)).build().inject(this);

    }

    @Override
    public void call(View v) {
        super.call(v);
        switch (v.getId()) {
            case R.id.btn_pay_gain_code:
                //获取验证码
                mSetPayPasswordPresenter.verifyPhone(mUserPhoneNumber);
                break;
            case R.id.bt_pay_conform:
                confirmPassword();
break;
        }
    }

    private void initUserInfo() {
        //设置用户的保密的电话号码
        Intent intent = getIntent();
        mUserPhoneNumber = intent.getStringExtra(JUMP_USER_PHONE_NUMBER);
        mEtPaySettingTitle.setText(String.format(getString(R.string.pay_password_settings_title),
                mUserPhoneNumber.substring(0, 3)
                        + "****" + mUserPhoneNumber.substring(7)));

    }


    private void initRXClick() {
        RxUtils.bindEvents(mBtnPayGainCode, this);
        RxUtils.bindEvents(mBtPayConform, this);
    }

    private void initId() {
        mEtPaySettingTitle = getViewById(R.id.et_pay_setting_title);
        mEtPayInputCode = getViewById(R.id.et_pay_input_code);
        mBtnPayGainCode = getViewById(R.id.btn_pay_gain_code);
        mEtPayPassword1 = getViewById(R.id.et_pay_password1);
        mEtPayPassword2 = getViewById(R.id.et_pay_password2);
        mBtPayConform = getViewById(R.id.bt_pay_conform);
    }
    /**
     * 发送验证码成功，验证验证码
     */

    public void confirmPassword() {
        String mEtPassword1 = mEtPayPassword1.getText().toString().trim();
        String mEtPassword2 = mEtPayPassword2.getText().toString().trim();
        String SmsVerifyCode = mEtPayInputCode.getText().toString().trim();
        if (TextUtils.isEmpty(mEtPassword1)){
            TipUtils.showToast(App.getInstance(),"密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(mEtPassword2)){
            TipUtils.showToast(App.getInstance(),"密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(SmsVerifyCode)){
            TipUtils.showToast(App.getInstance(),"验证码不能为空");
            return;
        }

        //验证是否为数字
        Boolean pw1IsCorrect = mSetPayPasswordPresenter.isAllNumber(mEtPassword1);
        Boolean pw2IsCorrect = mSetPayPasswordPresenter.isAllNumber(mEtPassword2);
        if (!(pw1IsCorrect && pw2IsCorrect)){
            TipUtils.showToast(App.getInstance(),"请输入6位数字的密码");
            return;
        }
        if (!mEtPassword1.equals(mEtPassword2)){
            TipUtils.showToast(App.getInstance(),"两次输入的密码不一致");
            return;
        }

        mSetPayPasswordPresenter.submitPassword(mEtPassword1,SmsVerifyCode,mUserPhoneNumber);



    }

    @Override
    protected int setView() {
        return R.layout.activity_set_pay_password;
    }

    @Override
    protected void onRefreshData() {

    }

    @Override
    public void showError(String msg) {

    }


    @Override
    public void setPasswordSucc() {
        finish();
    }
}
